#ifndef __SPO2_HPP
#define __SPO2_HPP

// spo2.hpp
// 包含从一系列颜色数据计算血氧饱和度的函数

#include <valarray>
#include <algorithm>
#include "misc_processing.hpp"

// 计算血氧饱和度
// 输入应该是指向cv::Vec3x的迭代器
template <typename IT>
double get_spo2(IT begin, IT end);

template <typename IT>
double get_spo2(IT begin, IT end) {
    std::vector<double> red, blue, green;

    for(IT p=begin; p<end; ++p) {
        blue.push_back((*p)[0]);
        green.push_back((*p)[1]);
        red.push_back((*p)[2]);
    }

    double ac1 = std_dev(blue.begin(), blue.end());
    double ac2 = std_dev(red.begin(), red.end());
    double dc1 = average<double>(blue.begin(), blue.end());
    double dc2 = average<double>(red.begin(), red.end());
    double r = (ac2/dc2) / (ac1/dc1);

    return -0.0063*r*r - 0.052*r + 1.038;
}

#endif // __SPO2_HPP